package com.xjgc.eip.twice.zxjeip2csjjcss.controller;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjgc.common.constant.Consants;
import com.xjgc.common.entity.ReturnResult;
import com.xjgc.common.enums.ReturnStatus;
import com.xjgc.eip.twice.zxjeip2csjjcss.entity.ZxjEip2cSjjcss;
import com.xjgc.eip.twice.zxjeip2csjjcss.service.ZxjEip2cSjjcssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XXX
 * @since 2019-12-06
 */
@Api("波峰焊回流焊时间实时监控信息抓取接口")
@RestController
@RequestMapping("/apis/zxjEip2cSjjc")
public class ZxjEip2cSjjcssController {
    @Autowired
    ZxjEip2cSjjcssService zxjEip2cSjjcssService;
    /**
     * 波峰焊回流焊时间实时监控信息抓取接口
     */
    @ApiOperation("波峰焊回流焊时间实时监控信息抓取接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "amount", value = "查询条数",
                    required = true, dataType = "int")
    })
    @GetMapping(value = "/getData")
    @ResponseBody
    public ReturnResult getData(Integer amount,@RequestHeader(name = "supplierCode",required = false)String supplierCode) {
        ReturnResult returnResult = new ReturnResult();
        QueryWrapper<ZxjEip2cSjjcss> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ioflag", Consants.Unread)
                .last("limit " + (amount == null ? 100 : amount));
        if(StringUtils.isNotBlank(supplierCode)){
            queryWrapper.eq("supplier_code",supplierCode);
        }
        try {
            List<ZxjEip2cSjjcss> result = zxjEip2cSjjcssService.list(queryWrapper);
            //更新查询的数据为已读
            if (result != null && result.size() > 0) {
                for (ZxjEip2cSjjcss zed : result) {
                    zed.setIoflag(Consants.read);
                    zed.setUpdateTime(new Date());
                }
                if (zxjEip2cSjjcssService.updateBatchById(result)) {
                    returnResult.setStatus(Integer.valueOf(ReturnStatus.OK.getCode()));
                    returnResult.setData(result);
                } else {
                    returnResult.setStatus(Integer.valueOf(ReturnStatus.FAIL.getCode()));
                    returnResult.setMessage("请求异常");
                }
            }
        } catch (Exception e) {
            returnResult.setStatus(Integer.valueOf(ReturnStatus.FAIL.getCode()));
            returnResult.setMessage("请求异常");
        }
        return returnResult;
    }
    @GetMapping(value = "/list")
    @DS("proxyDs")
    public Result<?> queryPageList(ZxjEip2cSjjcss zxjEip2cSjjcss,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<ZxjEip2cSjjcss> queryWrapper = QueryGenerator.initQueryWrapper(zxjEip2cSjjcss, req.getParameterMap());
        Page<ZxjEip2cSjjcss> page = new Page<ZxjEip2cSjjcss>(pageNo, pageSize);
        IPage<ZxjEip2cSjjcss> pageList = zxjEip2cSjjcssService.page(page, queryWrapper);
/*
        IPage<ZxjEip2cSjjcss> pageList = zxjEip2cSjjcssService.queryPageList( zxjEip2cSjjcss,pageNo,pageSize);
*/
        return Result.ok(pageList);
    }
}

